package com.bitcat.animationdemo;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;



public class MainActivity extends AppCompatActivity {

    private View content;

    private ImageView frameIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
//        content = findViewById(R.id.content);
        frameIv = findViewById(R.id.frameIv);
    }

    public void playAlphaAnim(View view) {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha_animation);
        content.startAnimation(animation);
    }

    public void playScaleAnim(View view) {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale_animation);
        content.startAnimation(animation);
    }

    public void playTranslateAnim(View view) {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate_animation);
        content.startAnimation(animation);
    }

    public void playRotateAnim(View view) {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_animation);
        content.startAnimation(animation);
    }

    public void playFrameAnim(View view) {
        // --帧动画
        //绑定动画
        frameIv.setBackgroundResource(R.drawable.frame_animation);
        //获取动画
        AnimationDrawable animationDrawable = (AnimationDrawable) frameIv.getBackground();


        // --属性动画
        // 创建属性动画对象，指定要动画化的属性（这里是translationX）
        ObjectAnimator animator = ObjectAnimator.ofFloat(frameIv, "translationX", 0f, 300f);

        // 设置动画时长
        animator.setDuration(2000);

        // 设置插值器，这里使用加速减速插值器
        animator.setInterpolator(new BounceInterpolator());

        // 动画重复次数，设置为-1表示无限重复
        animator.setRepeatCount(ValueAnimator.INFINITE);

        // 设置动画重复模式，这里设置为反向重复
        animator.setRepeatMode(ValueAnimator.REVERSE);



        // 启动动画
        animator.start();
        //播放动画
        animationDrawable.start();
    }
}